﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Security.Policy;

namespace com.uplus.common
{
    /// <summary>
    /// 文件管理服务
    /// </summary>
    public class FileMgr
    {
        #region 文件/目录 信息
        static string _dataPath = null;
        /// <summary>
        /// 获取Data文件夹位置, 如:\\Higo-ZCL\Higo\Data
        /// </summary>
        public static string DataPath
        {
            get
            {
                if (_dataPath == null)
                {
                    _dataPath = AppPath + @"\Data";
                }
                return _dataPath;
            }
        }

        static string _appPath = null;
        /// <summary>
        /// 获取程序文件夹位置, 如:\\Higo-ZCL\Higo
        /// </summary>
        public static string AppPath
        {
            get
            {
                if (_appPath == null)
                {
                    String con = AppConfigurtaion.GetFirstSection("con");
                    //_appPath = con.Contains("server=Higo-ZCL") ? @"\\Higo-ZCL\Higo" : @"C:\Higo";
                    _appPath = @"C:\Higo";
                }
                return _appPath;
            }
        }
        #endregion

        #region 文件/目录 添加/复制
        /// <summary>
        /// 以复制的形式添加文件(至默认目录,文件名格式:yyyyMMddHHmmssfff.后缀)
        /// </summary>
        /// <param name="source">文件来源(包括路径)</param>
        /// <returns>生成的文件名(不包括路径)</returns>
        public static string Add(string source)
        {
            // 1.生成文件名
            string exp = source.Substring(source.LastIndexOf('.') + 1);
            string safeName = string.Format("{0:yyyyMMddHHmmssfff}.{1}", DateTime.Now, exp);

            // 2.复制文件
            File.Copy(source, string.Format(@"{0}\{1}", DataPath, safeName));
            return safeName;
        }

        /// <summary>
        /// 强制复制源目录至目标目录
        /// </summary>
        /// <param name="sourcePath">目录路径</param>
        /// <param name="toPath">目标路径</param>
        public static void CopyAnyall(string sourcePath, string toPath)
        {
            string s = sourcePath.EndsWith("\\") ? sourcePath : sourcePath + "\\";
            string t = toPath.EndsWith("\\") ? toPath : toPath + "\\";

            if (!Directory.Exists(t)) Directory.CreateDirectory(t);
            foreach (string item in Directory.GetFiles(s))
            {
                string file = item.Replace(s, t);
                if (File.Exists(file)) DeleteAnyall(file); // 强制删除
                File.Copy(item, file);
            }

            foreach (string path in Directory.GetDirectories(s))
            {
                string newPath = path.Replace(s, t);
                if (!Directory.Exists(newPath))
                {
                    Directory.CreateDirectory(path);
                }
                CopyAnyall(path, newPath);
            }
        }

        /// <summary>
        /// 创建N层目录
        /// </summary>
        /// <param name="path">要创建的目录</param>
        public static void DirectoryCreat(string path)
        {
            if (path.EndsWith("\\")) path = path.TrimEnd('\\');

            if (!Directory.Exists(path))
            {
                string t = path.Substring(0, path.LastIndexOf('\\'));
                DirectoryCreat(t);
                Directory.CreateDirectory(path);
            }
        }
        #endregion

        #region 文件/目录 删除
        /// <summary>
        /// 在默认目录删除指定文件
        /// </summary>
        /// <param name="safeName">文件名(不包括路径)</param>
        public static void Delete(string safeName)
        {
            File.Delete(string.Format(@"{0}\{1}", DataPath, safeName));
        }

        /// <summary>
        /// 强制删除文件（包括只读）
        /// </summary>
        /// <param name="path">文件路径</param>
        public static void DeleteAnyall(string path)
        {
            FileInfo fi = new FileInfo(path);
            if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                fi.Attributes = FileAttributes.Normal;
            File.Delete(path);
        }

        /// <summary>
        /// 清除某个目录下的所有资源
        /// </summary>
        /// <param name="path"></param>
        public static void ClearPath(string path)
        {
            if (Directory.Exists(path))
            {
                foreach (string item in Directory.GetDirectories(path)) Directory.Delete(item, true);
                foreach (string item in Directory.GetFiles(path)) File.Delete(item);
            }
        }
        #endregion

        #region 文件/目录 统计
        /// <summary>
        /// 获取某个目录下所有文件数量（包括子目录）
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static int GetTotalFile(string path)
        {
            int n = 0;
            string[] fs = Directory.GetFiles(path);
            n = fs == null ? 0 : fs.Length;

            foreach (string item in Directory.GetDirectories(path))
            {
                n += GetTotalFile(item);
            }
            return n;
        }

        /// <summary>
        /// 当前目录中是否存在文件
        /// </summary>
        /// <param name="path">相对路径,如abc\abc.exe</param>
        /// <returns></returns>
        public static bool ExistsCurrent(string path)
        {
            return File.Exists(string.Format(@"{0}\{1}", AppPath, path));
        }
        #endregion

        #region 版本相关
        /// <summary>
        /// 获取文件版本号
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetProductVersion(string path)
        {
            FileVersionInfo fv = FileVersionInfo.GetVersionInfo(path);
            return fv.ProductVersion;
        }

        /// <summary>
        /// 比较两个文件版本是否一定
        /// </summary>
        /// <param name="path1"></param>
        /// <param name="path2"></param>
        /// <returns></returns>
        public static bool IsSameVersion(string path1, string path2)
        {
            return GetProductVersion(path1) == GetProductVersion(path2);
        }
        #endregion


        #region  获取所有文件
        /// <summary>
        /// 
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="FileList">文件</param>
        /// <returns></returns>
        public static List<string> GetFiles(string path, List<string> FileList)
        {
            DirectoryInfo dir = new DirectoryInfo(path);
            FileInfo[] fil = dir.GetFiles();
            DirectoryInfo[] dii = dir.GetDirectories();
            foreach (FileInfo f in fil)
            {
                string filename = f.FullName;
                FileList.Add(filename);
                //if (filename.EndsWith("jpg") | filename.EndsWith("JPG"))
                //{
                //    FileList.Add(filename);
                //}
            }
            //获取子文件夹内的文件列表，递归遍历  
            foreach (DirectoryInfo d in dii)
            {
                GetFiles(d.FullName, FileList);
            }
            return FileList;
        }
        #endregion

        #region 获取文件大小

       public static long GetFileSize(string fileUri)
        {
            FileInfo fi = new FileInfo(fileUri);
            return fi.Length;
        }
        #endregion
    }
}
